package com.j180.controller.supplier;

import com.j180.bean.PageBean;
import com.j180.bean.ResultBean;
import com.j180.bean.Supplier;
import com.j180.service.supplier.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sup")
public class SupplierController {

    @Autowired
    private ISupplierService iSupplierService;

    @GetMapping("/{curPage}/{pageSize}")
    private ResultBean<PageBean<Supplier>> showSuppliers(@PathVariable String curPage, @PathVariable String pageSize){

        int cur = Integer.parseInt(curPage);
        int size = Integer.parseInt(pageSize);

        ResultBean<PageBean<Supplier>> resultBean = new ResultBean<>();
        PageBean<Supplier> pageBean = iSupplierService.showAllSuppliers(cur, size);
        if(pageBean.getDatas() != null){
            resultBean.setT(pageBean);
            resultBean.setCode(1);
            resultBean.setMsg("操作成功，返回" + pageBean.getDatas().size() + "条数据");
        }else {
            resultBean.setCode(0);
            resultBean.setMsg("操作失败");
        }

        return resultBean;
    }

    @GetMapping("/{supId}")
    public ResultBean<Supplier> showSupplier(@PathVariable String supId){

        int id = Integer.parseInt(supId);

        ResultBean<Supplier> resultBean = new ResultBean<>();
        Supplier supplier = iSupplierService.showSupplierById(id);
        if(supplier != null){
            resultBean.setCode(1);
            resultBean.setT(supplier);
            resultBean.setMsg("操作成功");
        }else {
            resultBean.setCode(0);
            resultBean.setMsg("操作失败");
        }

        return resultBean;
    }

    @PostMapping("/add")
    public ResultBean addSupplier(@RequestBody Supplier supplier){

        ResultBean resultBean = new ResultBean();
        int result = iSupplierService.addSupplier(supplier);
        if(result > 0){
            resultBean.setCode(1);
            resultBean.setMsg("操作成功");
        }else {
            resultBean.setCode(0);
            resultBean.setMsg("操作失败");
        }

        return resultBean;
    }

    @DeleteMapping("/{supId}")
    public ResultBean deleteSupplier(@PathVariable String supId){
        int id = Integer.parseInt(supId);
        ResultBean resultBean = new ResultBean();
        int result = iSupplierService.deleteSupplier(id);
        if(result > 0){
            resultBean.setCode(1);
            resultBean.setMsg("操作成功");
        }else {
            resultBean.setCode(0);
            resultBean.setMsg("操作失败");
        }

        return resultBean;
    }

    @PutMapping("update")
    public ResultBean updateSupplier(@RequestBody Supplier supplier){
        ResultBean resultBean = new ResultBean();
        int result = iSupplierService.updateSupplier(supplier);
        if(result > 0){
            resultBean.setCode(1);
            resultBean.setMsg("操作成功");
        }else {
            resultBean.setCode(0);
            resultBean.setMsg("操作失败");
        }

        return resultBean;
    }
}
